package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author ：zboy
 * @date ：
 * @description ：
 * @version: 1.0
 */
@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {

    @Reference
    private OrderSettingService orderSettingService;

    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile){
        System.err.println("==================="+excelFile);
        System.err.println("==================="+excelFile.getOriginalFilename());
        try{
            // 从上传组件中读取文件，利用POI工具解析内容
            List<String[]> list = POIUtils.readExcel(excelFile);
            if (list != null && list.size() > 0){
                List<OrderSetting> orderSettingList = new ArrayList<>();
                // 从excel遍历数据，封装为设置对象，添加到集合
                for (String[] rowData:list){
                    System.err.println(rowData);
                    if(rowData[0] == null){
                        continue;
                    }
                    OrderSetting orderSetting = new OrderSetting(new Date(rowData[0]),Integer.parseInt((rowData[1])));
                    orderSettingList.add(orderSetting);
                }
                // 调用业务对象批量保存数据
                orderSettingService.add(orderSettingList);
                return new Result(true, MessageConst.IMPORT_ORDERSETTING_SUCCESS);
            }else{
                throw new RuntimeException("数据不能为空");
            }

        }catch(Exception e){
            e.printStackTrace();
            return new Result(false,e.getMessage());
        }
    }

}
